import api from '../../utils/api'
import { login } from '../../utils/conf'

Page({
  data: {
    name: '',
    password: ''
  },

  onNameInput(e) {
    const { value } = e.detail
    this.setData({ name: value })
  },
  onPasswordInput(e) {
    const { value } = e.detail
    this.setData({ password: value })
  },

  clearPasswordInput: function () {
    this.setData({
      password: "",
    });
  },
  clearNameInput: function () {
    this.setData({
      name: "",
    });
  },

  onLoginTap() {
    const { password, name } = this.data
    wx.showLoading({
      title: '加载中',
    })
    api.post(login, {
      name: JSON.stringify(name),
      password: JSON.stringify(password)
    }).then(res => {
      // console.log(res)
      wx.setStorage({
        key: "user",
        data: res.data
      })
      wx.setStorageSync('Authorization', res.data.session_id)
      wx.switchTab({ url: '/pages/index/index' })
    }).catch(err => {
      // console.log(err)
      wx.showToast({
        title: err.msg,
        icon: 'none',
        duration: 2000
      })
    })
    wx.hideLoading()
  }
});